// pages/addaddress/addaddress.js
const api = require("../../utils/api.js").default;
const myRequest = require("../../utils/request.js").default;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    parentId : 1,
    cityid: 0,
    districtid: 0,
    province : [],
    city : [],
    district : [],
    name : "",
    mobile : "",
    address:"",
    provinceText : "省份",
    cityText: "市区",
    districtText: "行政区域",
    isdefault : false,
    uporadd:false,
    addressId : 0,
  },

  // 保存更新地址功能
  saveaddres(){
    const that = this;
    if (this.data.uporadd){
      myRequest(api.address_save, "POST", {
        id: that.data.addressId,
        name: that.data.name,
        mobile: that.data.mobile,
        province_id: that.data.parentId,
        city_id: that.data.cityid,
        district_id: that.data.districtid,
        address: that.data.address,
        is_default: that.data.isdefault
      }, function (res) {
        console.log(res);
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000,
          success(res) {
            console.log(res);
            that.goaddres();
          }
        })
      });
    }else{
      myRequest(api.address_save, "POST", {
        name: that.data.name,
        mobile: that.data.mobile,
        province_id: that.data.parentId,
        city_id: that.data.cityid,
        district_id: that.data.districtid,
        address: that.data.address,
        is_default: that.data.isdefault
      }, function (res) {
        console.log(res);
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000,
          success(res) {
            console.log(res);
            that.goaddres();
          }
        })
      });
    }
  },


  // 获取收货人名字
  getname(e){
    this.setData({
      name: e.detail.value
    });
    console.log("获取收货人名字", this.data.name);
  },

  // 获取手机号码功能
  getphone(e){
    this.setData({
      mobile : e.detail.value      
    });
    console.log("获取手机号码功能", this.data.mobile);
  },


  // 完成选择
  finishch(e){
    const that = this;
    var districtindex = e.detail.value;
    var districtid = this.data.district[districtindex].id;
    this.setData({
      districtText: that.data.district[districtindex].name,
      districtid: districtid
    });
    console.log("选择完成：" + this.data.parentId + "市区"+this.data.cityid +"区域"+this.data.districtid)
  },

  // 填写街道完成
  inputfinish(e){
    this.setData({
      address:e.detail.value
    });
    console.log("街道完成", this.data.address);
  },

  // 是否默认
  isdefault(e){
    this.setData({
      isdefault: e.detail.value
    })
    console.log("是否默认",this.data.isdefault);
  },

  // 获取市区ID 
  getcityId(e){
    const that = this;
    var cityindex = e.detail.value;
    var provinceId = this.data.province[cityindex].id;
    myRequest(api.region, "GET", {
      parentId: provinceId
    }, function (res) {
      that.setData({
        city: res.data.data,
        provinceText: that.data.province[cityindex].name,
        parentId: provinceId
      })
      console.log("parentId", that.data.parentId);
    });
  },

  // 获取区ID 
  getdistrictId(e) {
    const that = this;
    var districtindex = e.detail.value;
    var cityid = this.data.city[districtindex].id;
    myRequest(api.region, "GET", {
      parentId: cityid
    }, function (res) {
      that.setData({
        district: res.data.data,
        cityText: that.data.city[districtindex].name,
        cityid: cityid
      })
      console.log("districtid", that.data.districtid);
    });
  },

  // 返回到上一页
  goaddres(){
    wx.navigateBack({
      delta : 1
    })
  },

  // 查看收货地址详情
  getdetail(e) {
    const that = this;
    var addid = e.id;
    myRequest(api.address_detail, "GET", { id: addid }, function (res) {
      console.log("详细",res.data.data);
      that.setData({
        addressId: res.data.data.id,
        name: res.data.data.name,
        mobile: res.data.data.mobile,
        parentId: res.data.data.province_id,
        cityid: res.data.data.city_id,
        districtid: res.data.data.district_id,
        address: res.data.data.address,
        isdefault: res.data.data.is_default,
        provinceText: res.data.data.province_name,
        cityText: res.data.data.city_name,
        districtText: res.data.data.district_name,
      });
      console.log(that.data)
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    console.log(options);
    if ("id" in options){
      this.setData({
        uporadd : true
      });
      this.getdetail(options)
    }
    console.log(this.data.uporadd)
    // 获取省份
    myRequest(api.region,"GET",{
      parentId: that.data.parentId
    },function(res){
      that.setData({
        province : res.data.data
      })
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})